﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows.Forms;
using GatewayInspect.Common;
using GatewayInspect.Model;
using System.Threading;

namespace GatewayInspect.Modules
{
    public partial class CLGLModule : BaseModule
    {
        private ProgressBarForm _processBar = null;
        private string _selectedFileName = string.Empty;

        public CLGLModule()
        {
            InitializeComponent();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            this.tsBtnSave.Visible = false;
        }

        protected override void OnLoadModule()
        {
            base.OnLoadModule();

            string strSql = "select * from T_TRUCK_CERT_INFO";
            //List<string> listWhere = new List<string>();
            //if (!string.IsNullOrWhiteSpace(tbxDriverID.Text))
            //{
            //    listWhere.Add("DRIVERID like '%" + tbxDriverID.Text + "%'");
            //}
            //if (!string.IsNullOrWhiteSpace(tbxTruckNo.Text))
            //{
            //    listWhere.Add("TRUCKNO like '%" + tbxTruckNo.Text + "%'");
            //}
            //if (listWhere.Count > 0)
            //{
            //    strSql += " and " + string.Join(" and ", listWhere);
            //}
            if (!string.IsNullOrWhiteSpace(tbxTruckNo.Text))
            {
                strSql += " where TRUCKNO like '%" + tbxTruckNo.Text + "%'";
            }
            this.cdgvTruck.AutoGenerateColumns = false;
            this.cdgvTruck.DataSource = DataAccess.ExecuteDataTable(strSql);
        }

        protected override void OnNew()
        {
            base.OnNew();

            using (CLGLForm form = new CLGLForm())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("新增车辆证件信息成功！");
                    OnLoadModule();
                }
            }
        }

        protected override void OnModify()
        {
            base.OnModify();

            if (cdgvTruck.SelectedCells != null && cdgvTruck.SelectedCells.Count > 0)
            {
                string strID = cdgvTruck.SelectedCells[0].OwningRow.Cells["ID"].Value.ToString();
                TruckCertInfo info = TruckCertInfo.GetInstanceFromDB(strID);
                using (CLGLForm form = new CLGLForm(info))
                {
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("修改车辆证件信息成功！");
                        OnLoadModule();
                    }
                }
            }
        }

        protected override void OnDelete()
        {
            base.OnDelete();
            if (cdgvTruck.SelectedCells != null && cdgvTruck.SelectedCells.Count > 0)
            {
                if (MessageBox.Show("确定要删除选中数据吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string strID = cdgvTruck.SelectedCells[0].OwningRow.Cells["ID"].Value.ToString();
                    int i = DataAccess.ExecuteNonQuery("delete from T_TRUCK_CERT_INFO where ID = '" + strID + "'");
                    if (i > 0)
                    {
                        MessageBox.Show("删除成功！");
                        OnLoadModule();
                    }
                }
            }
        }

        protected override void OnImportExcel()
        {
            base.OnImportExcel();

            OpenFileDialog _dialog = new OpenFileDialog();
            _dialog.Filter = "Excel文件|*.xls;*.xlsx|所有文件|*.*";
            _dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            _dialog.Multiselect = false;
            _dialog.Title = "从Excel文件导入";
            if (_dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            try
            {
                _selectedFileName = _dialog.FileName;
                Thread proBarThread = new Thread(new ThreadStart(DoImportExcel));
                proBarThread.Start();//显示进度条

                using (_processBar = new ProgressBarForm())
                {
                    _processBar.ShowDialog();
                }
                MessageBox.Show("导入完成！");
                OnLoadModule();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                OnLoadModule();
            }
        }

        private void DoImportExcel()
        {
            Thread.Sleep(1000);
            try
            {
                DataTable dtExcel = ModuleUtils.GetDataTableFromExcel(_selectedFileName, true);
                if (dtExcel != null && dtExcel.Rows.Count > 0)
                {
                    foreach (DataRow row in dtExcel.Rows)
                    {
                        string strTruckNo = row["车牌号"].ToString();
                        if (string.IsNullOrWhiteSpace(strTruckNo))
                            continue;

                        DataAccess.ExecuteNonQuery(string.Format("delete from T_TRUCK_CERT_INFO where TRUCKNO = '{0}'", strTruckNo));

                        string strSQL = "insert into T_TRUCK_CERT_INFO(ID,TRUCKNO,XSZ_CT_YXQ,XSZ_CG_YXQ,YSZ_CT_SYJL,YSZ_CG_SYJL,JYBG_YXQ,JYBG_JYDW,HD_WEIGHT,MHQ_YXQ,ORGCODE)" +
                            " values(:ID,:TRUCKNO,:XSZ_CT_YXQ,:XSZ_CG_YXQ,:YSZ_CT_SYJL,:YSZ_CG_SYJL,:JYBG_YXQ,:JYBG_JYDW,:HD_WEIGHT,:MHQ_YXQ,:ORGCODE)";
                        DataAccess.ExecuteNonQuery(strSQL, new OracleParameter(":ID", Guid.NewGuid().ToString()),
                            new OracleParameter(":TRUCKNO", row["车牌号"]),
                            new OracleParameter(":XSZ_CT_YXQ", GetDateValue(row["行驶证(车头)有效期"])) { DbType = DbType.Date },
                            new OracleParameter(":XSZ_CG_YXQ", GetDateValue(row["行驶证(车挂)有效期"])) { DbType = DbType.Date },
                            new OracleParameter(":YSZ_CT_SYJL", GetDateValue(row["道路运输证(车头)车辆审验及技术等级记录"])) { DbType = DbType.Date },
                            new OracleParameter(":YSZ_CG_SYJL", GetDateValue(row["道路运输证(车挂)车辆审验及技术等级记录"])) { DbType = DbType.Date },
                            new OracleParameter(":JYBG_YXQ", GetDateValue(row["罐体检验报告有效期"])) { DbType = DbType.Date },
                            new OracleParameter(":JYBG_JYDW", row["罐体检验报告检验单位"]),
                            new OracleParameter(":HD_WEIGHT", row["车辆载质量(吨)"]),
                            new OracleParameter(":MHQ_YXQ", GetDateValue(row["灭火器有效期"])) { DbType = DbType.Date },
                            new OracleParameter(":ORGCODE", Session.CurrentOrgCode));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                _processBar.DialogResult = DialogResult.OK;
            }
        }

        private object GetDateValue(object value)
        {
            return value == null || value.Equals("") ? DBNull.Value : value;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            OnLoadModule();
        }

        protected override void OnDownTemplate()
        {
            base.OnDownTemplate();
            ModuleUtils.DownUpFile("http://10.4.1.253/GatewayInspect/files/车辆证件信息.xlsx");
        }

        /*
        #region 读身份证

        private IDCradReader reader = null;

        protected override void OnInit()
        {
            base.OnInit();
            BeginRead();//启动身份证读卡设备开始读卡 
        }

        protected override void OnModuleDestroyed()
        {
            base.OnModuleDestroyed();
            if (reader != null)
            {
                reader.AfterReadInfo -= new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
                reader.CheckHandler();//检查是否还有其他对象读卡
            }
        }

        private void BeginRead()
        {
            if (reader == null)
            {
                reader = IDCradReader.GetInstance();
                reader.AfterReadInfo += new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
            }
            reader.BeginRead();
        }

        private void reader_AfterReadInfo(CardInfo info)
        {
            if (IsActive && info != null)
            {
                this.tbxDriverID.Text = info.CardNo;
            }
        }

        #endregion
        */
    }
}
